package com.sun.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.dto.SearchBlog;
import com.sun.pojo.Blog;
import com.sun.pojo.User;
import com.sun.service.BlogService;
import com.sun.service.TagService;
import com.sun.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: glorysun
 * @date: 2020/8/5
 */
@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    public void setTypeAndTag(Model model) {
        model.addAttribute("types", typeService.selectTypeList());
        model.addAttribute("tags", tagService.selectTagList());
    }

    //博客列表
    @GetMapping("/blogs")
    public String list(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,HttpSession session) {
        PageHelper.startPage(pageNum, 5);    //***要放在需要分类的查询语句之前！！！！！！！
        List<Blog> allBlog = blogService.selectBlogList();
        PageInfo<Blog> pageInfo = new PageInfo<>(allBlog);
        model.addAttribute("pageInfo", pageInfo);
        setTypeAndTag(model);
        SearchBlog searchBlog=new SearchBlog();
        model.addAttribute("searchBlog",searchBlog);
        return "admin/blogs";
    }

    //条件查询
    @GetMapping("/blogs/search")
    public String search(SearchBlog searchBlog, Model model,
                         @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum,HttpSession session) {
        if(searchBlog.getTitle()=="" &&
                searchBlog.getTypeId()==null && searchBlog.isRecommend()==false){
            return "redirect:/admin/blogs";
        }

        PageHelper.startPage(pageNum, 100);
        List<Blog> blogBySearch = blogService.selectBlogByCondition(searchBlog);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogBySearch);
        model.addAttribute("pageInfo", pageInfo);
        setTypeAndTag(model);
        model.addAttribute("message", "查询成功");
        model.addAttribute("search","查询");
        model.addAttribute("searchBlog",searchBlog);
        return "admin/blogs";
    }

    //博客新增页
    @GetMapping("/blogs/input")
    public String toInput(Model model){
        setTypeAndTag(model);
        return "admin/blogs-input";
    }

    //保存新增
    @PostMapping("/blogs/inputSave")
    public String input(Blog blog, HttpSession session, RedirectAttributes attributes){

        //设置用户信息
        blog.setUser((User) session.getAttribute("user"));
        blog.setUserId(blog.getUser().getId());
        //设置分类信息
        //blog.setType(blog.getType());
        blog.setTypeId(blog.getType().getId());
        //设置标签信息
        blog.setTagIds(blog.getTagIds());


        blogService.insertBlog(blog);


        attributes.addFlashAttribute("message","成功!");
        return "redirect:/admin/blogs";
    }

    //博客修改页
    @GetMapping("/blogs/edit/{id}")
    public String toEdit(@PathVariable("id") Long id,Model model){
        Blog blog=blogService.selectBlogById(id);
        setTypeAndTag(model);
        model.addAttribute("blog",blog);
        return "admin/blogs-edit";
    }

    //保存修改
    @PostMapping("/blogs/editSave")
    public String edit(Blog blog,RedirectAttributes attributes){
        blogService.updateBlog(blog);
        attributes.addFlashAttribute("message","修改成功!");
        return "redirect:/admin/blogs";
    }

    @GetMapping("/blogs/delete/{id}")
    public String delete(@PathVariable("id") Long id,RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功!");
        return "redirect:/admin/blogs";
    }

}
